#!/bin/sh
# Copyright (c) 2004 by SWsoft, Inc.
# All rights reserved
#
#ident  "@(#)plesk 7.0  04/02/02 SWsoft"

#
# Plesk locale pack install script
#

# accumulates chown and chmod
set_ac()
{
	u_owner="$1"
	g_owner="$2"
	perms="$3"
	node="$4"

	chown $u_owner:$g_owner $node || die "chown $u_owner:$g_owner $node"
	chmod $perms $node || die "chmod $perms $node"
}

p_echo()
{
	echo "$*" >> "$product_log" 2>&1
	echo "$*" 1>&2
}

pno_echo()
{
	echo "$*" >> "$product_log" 2>&1
}

pnnl_echo()
{
	if [ -z $necho_defined ]; then
		# set up echo
		case "`echo 'x\c'`" in
    		'x\c')
				echo="echo -n"
				nnl=
			;;
			x)
				echo="echo"
				nnl="\c"
			;;
			*)
				echo "ERROR: unable to set up echo."
			;;
		esac
		necho_defined=true
	fi

	$echo "$*${nnl}" >> "$product_log" 2>&1
	$echo "$*${nnl}"
}

die()
{
	printf "\a\a"
	p_echo
	p_echo "ERROR while trying to $*"
	p_echo "Check the error reason, fix it and try again"
	p_echo
	p_echo "Aborting..."
	p_echo

	exit 1
}

err()
{
	p_echo
	p_echo "ERROR!"
	p_echo "Check the error reason, fix it and try again"
	p_echo
	p_echo "Exiting..."
	p_echo

	exit 1
}

echo_try()
{
	msg="$*"
	pnnl_echo " Trying to $msg... "
}

suc()
{
	p_echo "done"
}

test_for_root()
{
	my_id="`id`"
	case "$my_id" in
		uid=0\(root\)*)
			;;
		*)	p_echo "$0: This script must be run with root privileges"
			p_echo "Become a root and run this script again"
			p_echo
			exit 1
			;;
	esac
}

chk_version()
{
	if [ -d "$product_root" ]; then
		product_real_ver="`cat $product_root/version | awk '{print $1}' -| awk -F'.' '{print $1"."$2}' -`"
		case "$product_real_ver" in
	    $product_version)
			;;
	    *)	p_echo
			p_echo "You have $product_bb version $product_real_ver"
			p_echo "In order for this script to work properly, please ensure that the version"
			p_echo "of $product_bb you're attempting to localizate is $product_version."
	    	p_echo "OK. Bye! See you later"
	    	p_echo
	    	exit 0
			;;
	    esac
	fi

p_echo
p_echo
p_echo
p_echo
p_echo	"\"$locale\" locale will be added to your control panel."
p_echo

if [ "$INTERACTIVE" = "NO" ]; then
	return
fi

pnnl_echo "Do you want to continue? [N] "

	read answer

	case "$answer" in
		y*|Y*)
			;;
		*)
			p_echo
			p_echo "OK. Bye! See you later"
			p_echo

			exit 0
			;;
	esac
	p_echo
}

get_etc_conf()
{
	if [ ! -f $config ]; then
		p_echo "ERROR: Unable to open $config file."
		exit 3
	fi

	product_root=`grep PRODUCT_ROOT_D $config | grep -v '^#' | awk '{print $2}'`
	if [ -z "$product_root" ]; then
		p_echo "ERROR: Unable to define $product root directory."
		exit 3
	fi

	rc_dir=`grep PRODUCT_RC_D $config | grep -v '^#' | awk '{print $2}'`
	if [ -z "$rc_dir" ]; then
		rc_dir=$product_root/rc.d
	fi

	mysql_bin_dir=`grep MYSQL_BIN_D $config | grep -v '^#' | awk '{print $2}'`
	if [ -z "$mysql_bin_dir" ]; then
		mysql_bin_dir=$product_root/mysql/bin
	fi
}

check_integrity()
{
#check of $product_bb intallation
if [ ! -d ${product_root} ]; then
	p_echo "ERROR: $product_bb directory '${product_root}' not found! Install $product_bb first."
	exit 1
fi

#check for free space
DISTSIZE=@LOCALE_PACKET_DISTSIZE@
FREESPACE=`df -kP ${product_root} | grep '\/dev\|vzfs'| awk '{ print $4 }'`

if [ "0${DISTSIZE}" -ge "0${FREESPACE}" ]; then
	p_echo "ERROR: Not enough disk space for installing the language pack."
	p_echo "ERROR: Not enough disk space for installing the language pack." >> $product_log
	exit 2
fi

}

extract_report()
{
	case $? in
		0)
			p_echo " Extraction successfully completed"
			p_echo
			;;
		*)

			p_echo
			p_echo "ERROR: something's wrong with distribution archive file"
			p_echo "Exiting..."
			p_echo

			exit 1
			;;
	esac
}

set_def_perms()
{
	chown -R root:0 "$install_dir/htdocs/locales/$locale/"
	chmod -R 644 "$install_dir/htdocs/locales/$locale/"
	find "$install_dir/htdocs/locales/$locale/" -type d -exec chmod 755 {} \;

	set_ac root 0 644 "$install_dir/plib/locales/$locale/conhelp_$locale.php"
	set_ac root 0 644 "$install_dir/plib/locales/$locale/messages_$locale.php"

	for dir in htdocs plib; do
		set_ac root 0 755 "$install_dir/$dir/locales"
		set_ac root 0 755 "$install_dir/$dir/locales/$locale"
	done
}

get_pid()
{
	proc_name="$1"

	case "$machine" in
	solaris)
		PID=`ps -ef | grep "$proc_name" | grep -v grep | awk '{print $2}'`
	;;
	*)
		PID=`ps axww | awk '$5 ~ "'$proc_name']*$" {print $1}'`
	;;
	esac
}

patch_symlink()
{
	if [ ! -d "$product_root/mysql/share/mysql" -a ! -L "$product_root/mysql/share/mysql" ]; then
		mkdir -p "$product_root/mysql/share" &&
		ln -sf /usr/share/mysql "$product_root/mysql/share/mysql" ||
		die "create symlink for MySQL default_charset fix"
	fi
}

f_help=NO
INTERACTIVE=YES

while [ -n "$1" ];  do
    case $1 in
	    --interactive)
			INTERACTIVE=YES
			;;
		--no-interactive)
			INTERACTIVE=NO
			;;
		--help | -h)
			f_help=YES
			;;
		*)
			echo $1: unknown argument
			exit 1
			;;
	esac

	shift
done

if [ "$f_help" = "YES" ]; then
	cat <<EOF
Usage: $0 [--interactive | --no-interactive]
EOF
	exit 0
fi

TARGET_OS="`uname -a`"
case "$TARGET_OS" in
	FreeBSD*i386)
		machine="BSD"
		uudecode="uudecode -p"
		;;
	Linux*2.?.*i?86*| \
	Linux*2.?.*x86_64*)
   			machine="linux"
   			uudecode="uudecode -o /dev/stdout"
		;;
	*)	echo "Not supported OS for patch"
		exit 1
		;;
esac


PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin
export PATH

my_name_0="`pwd`"
my_name_1="`basename ""$0""`"
my_name_2=`echo ""$0""|sed -e "s/^\.\///"`
case "$?" in
	0)
		my_name="$my_name_2"
	;;
	1)
		my_name="$my_name_0"/"$my_name_2"
	;;
    *)
		err
	;;
esac

###!!!!!! GLOBAL VARIABLES !!!!!###
product=psa
product_bb=Plesk

support_email=support@plesk.com
sales_email=sales@plesk.com

product_version="`echo "@PRODVERSION@" | awk '{print $1}' -| awk -F'.' '{print $1"."$2}' -`"
###!!!!!! end GLOBAL VARIABLES !!!!!###

product_log="/tmp/${my_name_1}.log"
config="/etc/$product/$product.conf"
test_for_root
get_etc_conf

install_dir="$product_root/admin"
admin_user="${product}adm"
admin_group="${product}adm"
admin_passwd="`cat /etc/$product/.psa.shadow`"
mysql="$mysql_bin_dir/mysql -N -uadmin -p$admin_passwd ${product}"

locale="@LOCALE@"

echo >> $product_log #initializing log
echo "START LOGGING: `date`" >> $product_log

check_integrity
chk_version

p_echo
p_echo	"So, it's time to unpack distribution files"
p_echo	"All files will be extracted to the $product_root directory"
p_echo	"This operation takes some time,"
p_echo	"please, wait..."
p_echo

p_echo "===> Extracting files ..."

cat "$my_name" | $uudecode | tar -C "$install_dir/" -xz -m -f -

extract_report

set_def_perms

get_pid "mysqld"
if [ -z "$PID" ]; then
	inten="start MySQL server"
	echo_try $inten
	$rc_dir/mysqld start >> $product_log 2>&1 &&
	suc || die $inten
	sleep 2
fi

res=`$mysql <<END
REPLACE INTO locales (id) VALUES('$locale');
END
`

if [ -z "$PID" ]; then
	inten="stop MySQL server"
	echo_try $inten
	$rc_dir/mysqld stop >> $product_log 2>&1 &&
	suc || die $inten
	sleep 2
fi

patch_symlink

printf "\a\a\a"
p_echo
p_echo
p_echo
p_echo
p_echo "                          Congratulations!"
p_echo "       All stages of the installation were successfully completed"
p_echo
p_echo
p_echo " If the number of LPs installed exceeds the number of possible LPs"
p_echo " allowed by the license key, then the installed locale exceeding"
p_echo " the number defined by the key will not be available via Plesk interface."
p_echo " Please contact your Plesk sales representative in order to purchase"
p_echo " an appropriate key."
p_echo
p_echo

exit 0
